package com.igd.action.user;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.igd.action.BaseAction;
import com.igd.business.service.IResService;
import com.igd.business.service.IRoleService;
import com.igd.common.constant.IConstant;
import com.igd.common.constant.RequestConstant;
import com.igd.exceptions.IgdException;
import com.igd.pojo.vo.ExtData;
import com.igd.security.Resources;
import com.igd.security.RoleResource;
import com.igd.security.Roles;
import com.igd.security.User;
import com.igd.utils.CommonUtil;
import com.igd.utils.DateUtil;
import com.igd.utils.ReflectionUtil;

/**
 * @createDate 2013-7-22
 *
 * @author QIAOYU
 *
 * @version 1.0
 *
 * @description 平台角色管理Action
 */

public class RoleAction extends BaseAction {
	
	private static final long serialVersionUID = 6562226865546708656L;
	
	private IRoleService roleService;
	
	private IResService resService;
	
	private Roles roles;
	
	private RoleResource roleRes;
	
	private String roleResourceStr;
	
	private final String ROLES_LIST_REQ_KEY = "ROLES_LIST_REQ_KEY";
	
	/**
	 * 跳转到角色管理页面
	 * 
	 * @return
	 * 
	 * @throws IgdException 
	 * 
	 * @throws SQLException 
	 */
	public String roleManager() throws SQLException, IgdException{
		
		this.getPage().setParam(this.pasrseReqParam());
		
		List<Roles> roleList = this.getRoleService().getAllRole(this.getPage());
		
		this.put(ROLES_LIST_REQ_KEY,roleList);
		
		return SUCCESS;
	}
	
	/**
	 * 跳转到角色添加页面
	 * 
	 * @return
	 * 
	 * @throws IgdException 
	 * 
	 * @throws SQLException 
	 * 
	 * @throws InvocationTargetException 
	 * 
	 * @throws IllegalAccessException 
	 * 
	 * @throws ClassNotFoundException 
	 * 
	 * @throws IllegalArgumentException 
	 * 
	 * @throws SecurityException 
	 */
	public String toRoleAdd() throws SQLException, IgdException, SecurityException, IllegalArgumentException, ClassNotFoundException, IllegalAccessException, InvocationTargetException{
		
		Resources res = new Resources();
		
		res.setResIsNode("1");
		
		Map<String,String> param = ReflectionUtil.objectToMap(Resources.class, res);
		
		this.getPage().setParam(param);
		
		List<Resources> resList = this.getResService().getAllRes(this.getPage());
		
		this.put(RequestConstant.REQ_RES_INFO,resList);
		
		return SUCCESS;
	}
	
	/**
	 * 跳转到角色修改页面
	 * 
	 * @return
	 * 
	 * @throws IgdException 
	 * 
	 * @throws SQLException
	 *  
	 * @throws InvocationTargetException 
	 * 
	 * @throws IllegalAccessException
	 *  
	 * @throws ClassNotFoundException 
	 * 
	 * @throws IllegalArgumentException 
	 * 
	 * @throws SecurityException 
	 */
	public String toRoleModify() throws SQLException, IgdException, SecurityException, IllegalArgumentException, ClassNotFoundException, IllegalAccessException, InvocationTargetException{
		
		Roles role = this.getRoleService().getRoleInfoByRoleId(this.getRoles().getRolesId());
		
		if(null != role){
			
			this.put(RequestConstant.REQ_ROLE_INFO, role);
			
			Map<String,String> param = ReflectionUtil.objectToMap(Roles.class, role);
			
			this.getPage().setParam(param);
			
			List<RoleResource> roleResources = this.getRoleService().getRoleResource(this.getPage());
			
			this.put(RequestConstant.REQ_ROLE_RESOURCES_INFO,roleResources);

		}

		return SUCCESS;
	}
	
	/**
	 * 角色添加处理
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @throws IgdException
	 */
	public String roleAdd() throws SQLException, IgdException{
		
		ExtData<Roles> extData = new ExtData<Roles>();
		
		if(null != this.getRoles()){
			
			Roles role = this.getRoleService().saveRole(this.getRoles());
			
			if(null != role && StringUtils.isNotEmpty(this.getRoleResourceStr())){
				
				String [] roleResArray = this.getRoleResourceStr().split(IConstant.SPLIT_KEY);
				
				if(null != roleResArray && roleResArray.length > 0){
					
					int roleResSize = roleResArray.length;
					
					List<RoleResource> roleResList = new ArrayList<RoleResource>(roleResSize);
					
					User currentUser = CommonUtil.getCurrentUser();
					
					for(int i=0;i<roleResSize;i++){
						
						String resId = roleResArray[i];
						
						if(StringUtils.isNotEmpty(resId)){
							
							RoleResource roleRes = new RoleResource();
							
							roleRes.setRoleId(role.getRolesId());
							
							roleRes.setResId(resId);
							
							roleRes.setRoleResCreateTime(DateUtil.getLongDate());
							
							roleRes.setRoleResCreateUser(currentUser.getUserId());

							roleResList.add(roleRes);

						}
					}
					
					boolean success = this.getRoleService().batchSaveRoleResource(roleResList);
					
					if(success){
						
						extData.setMessage("role.add.success");
						
					}else{
						
						extData.setStatusCode(ExtData.FAIL);
						
						extData.setMessage("role.add.fail");
						
					}
				}
			}
			
		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
			extData.setMessage("role.add.fail");

		}
		
		this.putExtData(extData);
		
		return this.JSON_PAGE;
	}
	
	/**
	 * 角色修改处理
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @throws IgdException
	 */
	public String roleModify() throws SQLException, IgdException{
		
		ExtData<Roles> extData = new ExtData<Roles>();
		
		if(null != this.getRoles()){
			
			this.getRoleService().updRoleByRoleId(this.getRoles());

			extData.setMessage("role.modify.success");

		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
			extData.setMessage("role.modify.fail");

		}
		
		this.putExtData(extData);
		
		return this.JSON_PAGE;
	}
	
	/**
	 * 角色删除处理
	 * 
	 * @return
	 * 
	 * @throws SQLException
	 * 
	 * @throws IgdException
	 */
	public String roleDel() throws SQLException, IgdException{
		
		ExtData<Roles> extData = new ExtData<Roles>();
		
		if(null != this.getRoles()){
			
			this.getRoleService().delRoleByRoleId(this.getRoles().getRolesId());

			extData.setMessage("role.del.success");

		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
			extData.setMessage("role.del.fail");

		}
		
		this.putExtData(extData);
		
		return this.JSON_PAGE;
	}
	
	
	public String roleResDel() throws SQLException, IgdException{
		
		ExtData<RoleResource> extData = new ExtData<RoleResource>();
		
		if(null != this.getRoleRes()){
			
			this.getRoleService().delRoleResourceByRoleResId(this.getRoleRes().getRoleResId());

			extData.setMessage("roleRes.del.success");

		}else{
			
			extData.setStatusCode(ExtData.FAIL);
			
			extData.setMessage("roleRes.del.fail");

		}
		
		this.putExtData(extData);
		
		return this.JSON_PAGE;
	}
	
	public IRoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public Roles getRoles() {
		return roles;
	}

	public void setRoles(Roles roles) {
		this.roles = roles;
	}

	public RoleResource getRoleRes() {
		return roleRes;
	}

	public void setRoleRes(RoleResource roleRes) {
		this.roleRes = roleRes;
	}

	public IResService getResService() {
		return resService;
	}

	public void setResService(IResService resService) {
		this.resService = resService;
	}

	public String getRoleResourceStr() {
		return roleResourceStr;
	}

	public void setRoleResourceStr(String roleResourceStr) {
		this.roleResourceStr = roleResourceStr;
	}
}
